package com.example.hewei.smarttransport.Activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout.LayoutParams;

import com.example.hewei.smarttransport.Adapter.ViewPagerAdapter;
import com.example.hewei.smarttransport.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 何伟 on 2017/3/7.
 */

public class GuideActivity extends BaseActivity implements ViewPager.OnPageChangeListener {
    private ViewPager mViewPager;
    private ViewPagerAdapter mAdapter;
    private List<Integer> mPictureList;
    private LinearLayout mPointSetLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        initData();
        initView();
        initPoint();
        setViewPager();
    }

    /**
     * 初始化数据添加图片资源
     */
    private void initData() {
        mPictureList = new ArrayList<Integer>();
        mPictureList.add(R.drawable.smart_1);
        mPictureList.add(R.drawable.smart_2);
        mPictureList.add(R.drawable.smart_3);
    }

    /**
     * 初始化控件
     */
    private void initView() {
        mPointSetLayout = (LinearLayout) findViewById(R.id.point_show_layout);
        mViewPager = (ViewPager) findViewById(R.id.picture_show_vp);
    }

    /**
     * 为ViewPager设置相应的参数
     */
    private void setViewPager() {
        mAdapter = new ViewPagerAdapter(this, mPictureList);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOnPageChangeListener(this);
        mViewPager.setCurrentItem(0);
    }

    /**
     * 初始化界面下面指示器的图片
     */
    private void initPoint() {
        // 清除所有的视图
        mPointSetLayout.removeAllViews();

        for (int i = 0; i < mPictureList.size(); i++) {
            ImageView mImageView = new ImageView(this);
            if (i == 0) {
                mImageView
                        .setBackgroundResource(R.drawable.point_red);
            } else {
                mImageView
                        .setBackgroundResource(R.drawable.point_white);
            }
            mImageView
                    .setLayoutParams(new LayoutParams(20, 20, Gravity.CENTER));
            LayoutParams mParams = new LayoutParams(new ViewGroup.LayoutParams(
                    50, 50));
            mParams.leftMargin = 50;
            mParams.rightMargin = 50;
            mPointSetLayout.addView(mImageView, mParams);
        }
    }

    /**
     * 滚动状态发生变化时调用
     *
     * @see android.support.v4.view.ViewPager.OnPageChangeListener#
     *      onPageScrollStateChanged(int)
     */
    @Override
    public void onPageScrollStateChanged(int arg0) {

    }

    /**
     * 当前页滚动时调用(不论是人为的触摸滚动还是程序自己执行的滚动)
     *
     *
     * @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageScrolled
     *      (int, float, int)
     */
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }

    /**
     * 新页面选中时调用
     *
     * @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageSelected
     *      (int)
     */
    @Override
    public void onPageSelected(int arg0) {
        selectPointShow(arg0);
    }

    /**
     * 选择显示的点
     *
     * @param index
     *            传入选择点亮指示器的序号
     */
    private void selectPointShow(int index) {
        for (int i = 0; i < mPointSetLayout.getChildCount(); i++) {
            ImageView mImageView = (ImageView) mPointSetLayout.getChildAt(i);
            if (i == index) {
                mImageView
                        .setBackgroundResource(R.drawable.point_red);
            } else {
                mImageView
                        .setBackgroundResource(R.drawable.point_white);
            }
        }
    }
}
